package Object.net.tjdz.service;
import Object.net.tjdz.model.Student;
import java.util.ArrayList;
import java.util.List;

public class StudentService implements PersonService<Student> {

    private final List<Student> studentDatabase = new ArrayList<>();

    @Override
    public boolean add(Student student) {

        if (getById(student.getId()) != null) {
            return false;
        }
        studentDatabase.add(student);
        return true;
    }

    @Override
    public boolean delete(String id) {
        Student student = getById(id);
        if (student == null) {
            return false;
        }
        return studentDatabase.remove(student);
    }

    @Override
    public boolean update(String id, Student newStudent) {
        Student oldStudent = getById(id);
        if (oldStudent == null) {
            return false;
        }
        oldStudent.setName(newStudent.getName());
        oldStudent.setAge(newStudent.getAge());
        return true;
    }

    @Override
    public Student getById(String id) {
        return studentDatabase.stream()
                .filter(student -> student.getId().equals(id))
                .findFirst()
                .orElse(null);
    }

    @Override
    public List<Student> getAll() {
        return new ArrayList<>(studentDatabase); // 返回副本避免外部修改
    }
}